#include<iostream>
using namespace std;

const int kMaxN = (int)1e6 + 10;
int father[kMaxN];
int len[kMaxN];

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    
    int T;
    
    for(cin >> T; T; T--){
        int n;
        int ans = 0;
        
        cin >> n;
        fill(len, len + n + 1, 0);
        
        for(int i = 2; i <= n; i++){
            cin >> father[i];
            len[father[i]] = n;
        }
        for(int i = n; i > 1; i--){
            len[father[i]] = min(len[father[i]], len[i] + 1); 
            ans = max(ans, len[i] + 1);
            //cout << i << ":" << len[i] << endl;
        }
        cout << max(ans, len[1]) << endl;
    }
    return 0;
}
